﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using weiqijiance;
using ZedGraph;
using CCWin;

namespace weiqijiance
{
    public partial class yure : CCSkinMain
    {

        public mainform m_mainform;
        public Thread t1;
        public Thread t2;


        double[] dtime = new double[10000];
        double[] dSP = new double[10000];
        double[] dPW = new double[10000];

       
        public int igcsj;
        DateTime dpinlv;
        public double dymax;

        public float m_fspeed;
        public float fForce1;

        public string sASMJGXZ;//是否进行结果修正
        public int iASMtdz;//ASM信号左通道
        public int iASMtdy;//ASM信号右通道
        public int iASMtdh;//ASM信号右通道
        public int iASMHWQ;//ASM到位红外

        public int iASMKZJSS;//ASM举升升
        public int iASMKZJSJ;//ASM举升降
        public int iASMSDtd;//车速信号接在那个通道
        //红外取反
        public bool[] bhongwaiqf = new bool[16] { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false };//红外是否取反
        //红外定位时间
        public int[] ihongwaidwsj = new int[16] { 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 };//红外定位时间单位毫秒

        public float fyuretime;
        public float time;
        public float timesy;
        public yure()
        {
            InitializeComponent();
            fyuretime = 3f;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

            x = this.Width;
            y = this.Height;
            setTag(this);
        }

        #region 控件大小随窗体大小等比例缩放
        private float x;//定义当前窗体的宽度
        private float y;//定义当前窗体的高度
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        #endregion

        public void gettongdao()
        {

            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);

            ///读取检定数据

            for (int i = 0; i < 10; i++)
            {
                string sgetname = m_mainform.ReadValue("通道设置", string.Format(@"模拟信号{0}", i + 1));
                if (sgetname.Contains("测功力左"))
                {
                    iASMtdz = i;
                }

                if (sgetname.Contains("测功力右"))
                {
                    iASMtdy = i;
                }

                if (sgetname.Contains("测功力后"))
                {
                    iASMtdh = i;
                }
            }

            for (int i = 0; i < 8; i++)
            {
                string smaichong = m_mainform.ReadValue("通道设置", string.Format(@"速度{0}", i + 1));
                if (smaichong.Contains("速度"))
                {
                    iASMSDtd = i;
                }

            }

            for (int i = 0; i < 16; i++)
            {
                string shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外{0}", i + 1));
                if (shongwai.Contains("测功机到位"))
                {
                    iASMHWQ = i;
                }


                shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外定位时间{0}", i + 1));
                int.TryParse(shongwai, out ihongwaidwsj[i]);


                shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外是否取反{0}", i + 1));
                if (shongwai.Contains("是"))
                {
                    bhongwaiqf[i] = true;
                }
                else
                {
                    bhongwaiqf[i] = false;
                }
            }
            ///控制
            for (int i = 0; i < 8; i++)
            {

                string shongwai = m_mainform.ReadValue("通道设置", string.Format(@"控制{0}", i + 1));
                if (shongwai.Contains("测功举升升"))
                {
                    iASMKZJSS = i;
                }
                if (shongwai.Contains("测功举升降"))
                {
                    iASMKZJSJ = i;
                }
            }
        }
        private delegate void FlushClient();//代理
       
        private void xianshitime()
        {
            if (this.text_time.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshitime);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_time.Text = string.Format(@"{0}", time);
            }
        }

        private void xianshitimeSY()
        {
            if (this.text_timesy.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshitimeSY);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_timesy.Text = string.Format(@"{0}", timesy);
            }
        }

        private void skinBut_ksjc_Click(object sender, EventArgs e)
        {
            gettongdao();
            m_mainform.DAC.tiaoling(1);//调零
            m_mainform.DAC.Lift(false);
            Thread.Sleep(1500);

            ///读取检定数据
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\设备检定结果.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);

            float.TryParse(m_mainform.ReadValue("预热", "预热时间"),out fyuretime);

            if (fyuretime <0.1)
            {
                fyuretime = 3f;
            }



            t1 = new Thread(new ThreadStart(Thread1));
            t1.Priority = ThreadPriority.BelowNormal;
            t1.Start();
        }
        public void Thread1()
        {
            try
            {
                testyure();
            }
            catch
            {

            }
        }
        public void Thread2()
        {



        }
        private double DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            double dateDiff = 0;
            TimeSpan ts = DateTime1.Subtract(DateTime2).Duration();
            dateDiff = ts.Days * 60 * 60 * 60 * 1000 + ts.Hours * 60 * 60 * 1000 + ts.Minutes * 60 * 1000 + ts.Seconds * 1000 + ts.Milliseconds;
            return dateDiff;
        }
        public void WriteGraph(ZedGraphControl zgc, double[] izdz, double[] izdy, int icount)
        {
            double imax = 0;
            for (int i = 0; i < icount; i++)
            {
                if (imax < izdz[i])
                {
                    imax = izdz[i];
                }
            }
            imax = imax * 1.1;
            for (int i = 0; i < icount; i++)
            {

                if (imax < izdy[i])
                {
                    imax = izdy[i];
                }

            }
            imax = imax * 1.1;

            GraphPane myPane = zgc.GraphPane;
            myPane.Fill = new Fill(Color.White, Color.Aqua, 45.0f);
            myPane.Legend.Fill = new Fill(Color.Gainsboro);
            myPane.Chart.Fill = new Fill(Color.Gainsboro);

            myPane.CurveList.Clear();

            myPane.Title.Text = string.Format("曲线");

            myPane.XAxis.Title.Text = "时间(s)";
            myPane.YAxis.Title.Text = "速度(km/h)";

            myPane.YAxis.MajorGrid.IsVisible = false;
            myPane.YAxis.MinorTic.Size = 0f;

            myPane.YAxis.Scale.Min = 0;
            myPane.YAxis.Scale.Max = imax + 0;
            myPane.YAxis.Scale.MajorStep = 10;

            zgc.IsShowPointValues = true;//数据节点
            string ss0 = string.Format("速度");
            string ss1 = string.Format("功率");

            PointPairList list0 = new PointPairList();
            PointPairList list1 = new PointPairList();


            LineItem myCurve0 = myPane.AddCurve(ss0, list0, Color.Blue,
                                    SymbolType.None);
            LineItem myCurve1 = myPane.AddCurve(ss1, list1, Color.Red,
                                    SymbolType.None);
            myCurve0.Line.Width = 1.7F;
            myCurve1.Line.Width = 1.7F;

            for (int i = 0; i < icount; i++)
            {
                list0.Add(i, izdz[i]);
                list1.Add(i, izdy[i]);
            }
            zgc.AxisChange();
            zgc.Invalidate();
        }

       
        public void testyure()//内阻
        {
            time = fyuretime * 60;
            xianshitime();
            igcsj = 0;
            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
            m_mainform.BianPinQi(true);
            Thread.Sleep(3000);
            m_mainform.BianPinQiSpeed(256 * 10);//设置频率 256 对应2.56

           
            DateTime dend50;

            DateTime dstartgcsj;

            dend50 = DateTime.Now;
            dstartgcsj = DateTime.Now;
            dpinlv = DateTime.Now;
            while(true)
            {
                Thread.Sleep(20);
                m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左",  m_mainform.DAC.fAIDataZero[iASMtdz]);
                float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右",  m_mainform.DAC.fAIDataZero[iASMtdy]);
                float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后",  m_mainform.DAC.fAIDataZero[iASMtdh]);

                fForce1 = fzou + fyou + fhou;
                if (fForce1 < 0)
                {
                    fForce1 = 0;
                }

                if (DateDiff(DateTime.Now, dend50) > 1000 * 60 * fyuretime)
                {
                    m_mainform.BianPinQi(false); //关闭电源电源
                    break;
                }

                if (DateDiff(DateTime.Now, dpinlv) > 500)
                {
                    timesy = (float)(fyuretime * 60 - DateDiff(DateTime.Now, dend50)*0.001);
                    xianshitimeSY();

                    dSP[igcsj] = (double)m_fspeed;
                    dPW[igcsj] = fForce1 * m_fspeed / 360;
                    dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;

                    for (int i = 0; i < igcsj; i++)
                    {
                        if (dymax < dSP[igcsj])
                        {
                            dymax = dSP[igcsj];
                        }
                        if (dymax < dPW[igcsj])
                        {
                            dymax = dPW[igcsj];
                        }
                    }

                    WriteGraph(zedGraphControl1, dSP, dPW, igcsj + 1);
                    igcsj++;
                    dpinlv = DateTime.Now;
                }


            }
            timesy = 0;
            xianshitimeSY();


            while (true)
            {
                Thread.Sleep(20);
                m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左",  m_mainform.DAC.fAIDataZero[iASMtdz]);
                float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右",  m_mainform.DAC.fAIDataZero[iASMtdy]);
                float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后",  m_mainform.DAC.fAIDataZero[iASMtdh]);

                fForce1 = fzou + fyou + fhou;
                if (fForce1 < 0)
                {
                    fForce1 = 0;
                }

                if (m_fspeed < 1)
                {
                    break;
                }

                if (DateDiff(DateTime.Now, dpinlv) > 500)
                {
                    dSP[igcsj] = (double)m_fspeed;
                    dPW[igcsj] = fForce1 * m_fspeed / 360;
                    dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;

                    for (int i = 0; i < igcsj; i++)
                    {
                        if (dymax < dSP[igcsj])
                        {
                            dymax = dSP[igcsj];
                        }
                        if (dymax < dPW[igcsj])
                        {
                            dymax = dPW[igcsj];
                        }
                    }

                    WriteGraph(zedGraphControl1, dSP, dPW, igcsj + 1);
                    igcsj++;
                    dpinlv = DateTime.Now;
                }
            }





            ///记录检定数据
            iniclass objini = new iniclass();
            objini.Ini(System.Windows.Forms.Application.StartupPath + "\\设备检定结果.ini");

            objini.Writue("台架预热", "判定", "合格");
            objini.Writue("台架预热", "检定时间", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));



            string ssqlex = string.Format(@"insert into JDSJ (
                                               JDSJ_MC,
                                               JDSJ_JG,
                                               JDSJ_RQ,
                                                JDSJ_JCX) values (
                                                '{0}',
                                                '{1}',
                                                '{2}',
                                                '{3}')",
                                         "预热",
                                         string.Format("预热结果:正常;检测线：{0}", m_mainform.lineCode),
                                         DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                                 m_mainform.lineCode);;
            m_mainform.objbassLW.objdb.InsertDB(ssqlex);
            //m_mainform.objbassLW.objdb.InsertDB(ssqlex);

             //m_mainform.DAC.Lift(true);

            MessageBox.Show("预热完成");


        }

        private void yure_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / x;
            float newy = (this.Height) / y;
            setControls(newx, newy, this);
        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.Lift(true);
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.Lift(false);
        }

    }
}
